home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / manual / examples / inetcli.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  1KB  |  60 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9.  
  10. #define PORT        5555
  11. #define MESSAGE        "Yow!!! Are we having fun yet?!?"
  12. #define SERVERHOST     "churchy.gnu.ai.mit.edu"
  13.  
  14. void 
  15. write_to_server (int filedes)
  16. {
  17.   int nbytes;
  18.  
  19.   nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
  20.   if (nbytes < 0)
  21.     {
  22.       perror ("write");
  23.       exit (EXIT_FAILURE);
  24.     }
  25. }
  26.  
  27.  
  28. int
  29. main (void)
  30. {
  31.   extern void init_sockaddr (struct sockaddr_in *name,
  32.                  const char *hostname,
  33.                  unsigned short int port);
  34.   int sock;
  35.   struct sockaddr_in servername;
  36.  
  37.   /* Create the socket.  */
  38.   sock = socket (PF_INET, SOCK_STREAM, 0);
  39.   if (sock < 0)
  40.     {
  41.       perror ("socket (client)");
  42.       exit (EXIT_FAILURE);
  43.     }
  44.  
  45.   /* Connect to the server.  */
  46.   init_sockaddr (&servername, SERVERHOST, PORT);
  47.   if (0 > connect (sock,
  48.            (struct sockaddr *) &servername,
  49.            sizeof (servername)))
  50.     {
  51.       perror ("connect (client)");
  52.       exit (EXIT_FAILURE);
  53.     }
  54.  
  55.   /* Send data to the server.  */
  56.   write_to_server (sock);
  57.   close (sock);
  58.   exit (EXIT_SUCCESS);
  59. }
  60.